package com.prs.crm.dao.crm.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.courser.hibernate.QueryExample;
import org.courser.ui.PaginationSupport;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.crm.DispatchBookDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.crm.Dispatch;
import com.prs.crm.domain.crm.DispatchBook;

public class DispatchBookDaoImpl extends BaseDaoImpl<DispatchBook> implements
		DispatchBookDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<DispatchBook> getAdressToDispatchBook(final String adress) {
		return (List<DispatchBook>) this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						Criteria criteria = arg0.createCriteria(DispatchBook.class);
						criteria.add(Restrictions.eq("boAdress", adress));
						return criteria.list();
					}
					
				}
			);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DispatchBook> getDateToDispatchBook(final String transportDate) {
		return (List<DispatchBook>) this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						Criteria criteria = arg0.createCriteria(DispatchBook.class);
						criteria.add(Restrictions.eq("boDate", transportDate));
						return criteria.list();
					}
					
				}
			);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DispatchBook> getNameToDispatchBook(final String name) {
		return (List<DispatchBook>) this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						Criteria criteria = arg0.createCriteria(DispatchBook.class);
						criteria.add(Restrictions.eq("boName", name));
						return criteria.list();
					}
					
				}
			);
	}



	@SuppressWarnings("unchecked")
	@Override
	public List<DispatchBook> getNumToDispatchBook(final String num) {
		return (List<DispatchBook>) this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						Criteria criteria = arg0.createCriteria(DispatchBook.class);
						criteria.add(Restrictions.eq("boohfRecod", num));
						return criteria.list();
					}
					
				}
			);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<DispatchBook> getNumpageToDispatchBook(final String num,final String page) {
		return (List<DispatchBook>) this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						Criteria criteria = arg0.createCriteria(DispatchBook.class);
						criteria.add(Restrictions.eq("boohfRecod", num));
						criteria.add(Restrictions.eq("bopageNum", page));
						return criteria.list();
					}
					
				}
			);
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<DispatchBook> getBookToByNameandByAdress(final String name,final String adress) {
		return (List<DispatchBook>) this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						Criteria criteria = arg0.createCriteria(DispatchBook.class);
						criteria.add(Restrictions.eq("boName", name));
						criteria.add(Restrictions.eq("boAdress", adress));
						return criteria.list();
					}
					
				}
			);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public DispatchBook getDispatchBook(final Dispatch dispatch) {
		return (DispatchBook) this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session arg0)
							throws HibernateException, SQLException {
						Criteria criteria = arg0.createCriteria(DispatchBook.class);
						criteria.add(Restrictions.eq("dispatch", dispatch));
						List<DispatchBook> alist=criteria.list();
						if(alist.size()==0){
							return null;
						}else{
						return alist.get(0);
						}
						}
					
				}
			);
	} 


	

}
